package gui.edit.components.dialogs;

import gui.edit.components.BezierComponent;
import gui.edit.enums.E_EditParamsSliders;
import gui.edit.listeners.ListenerFillParamOkButton;
import gui.edit.listeners.ListenerFillParamSliderEndChange;
import gui.edit.listeners.ListenerFillParamSliderInterpolationDownChange;
import gui.edit.listeners.ListenerFillParamSliderInterpolationUpChange;
import gui.edit.listeners.ListenerFillParamSliderStartChange;
import gui.edit.utils.QualitativeSliders;
import gui.utils.IntegerField;
import gui.visu.listeners.ListenerCancelButton;
import gui.visu.listeners.ListenerIntegerField;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.Hashtable;

import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import tuio.components.ButtonTouch;
import tuio.components.SliderTouch;

import controls.edit.ParamControl;
import data.Constants;
import data.visu.structures.ValueAndString;


/**
 * @author Charles Perin
 * 29 mars 2011
 * LIMSI - INRIA Rennes
 * charles.perin@free.fr
 *
 */
public class JDialogFillParam extends JDialog {

	private static final long serialVersionUID = -651982042721038870L;

	private SliderTouch startSlider;
	private SliderTouch endSlider;
	private SliderTouch interpolationSliderUp;
	private SliderTouch interpolationSliderDown;
	
	private JTextField startField;
	private JTextField endField;

	private BezierComponent bezierComponent;

	private JLabel startLabel;
	private JLabel endLabel;

	private JPanel optionsPanel;

	private E_EditParamsSliders editParam;

	private ParamControl paramControl;

	public ParamControl getParamControl() {
		return paramControl;
	}

	private Integer y1, y2;

	private int interpolationUpValue, interpolationDownValue;

	public JDialogFillParam(JFrame parent, String title, boolean modal, ParamControl paramControl, E_EditParamsSliders editParam) {
		super(parent, title, modal);
		this.editParam = editParam;
		this.paramControl=paramControl;
		y1 = new Integer(0);
		y2 = new Integer(0);
		this.setResizable(false);
		this.setUndecorated(true);
		this.initComponent();
		this.pack();
	}

	private void initComponent() {
		JPanel content = new JPanel(new BorderLayout());
		content.setBorder(BorderFactory.createMatteBorder(3, 3, 1, 3, Constants.WINDOW_BORDER_COLOR));
		content.add(buildOptionsPanel(), BorderLayout.CENTER);

		JPanel control = new JPanel();
		control.setBorder(BorderFactory.createMatteBorder(0, 3, 3, 3, Constants.WINDOW_BORDER_COLOR));
		ButtonTouch okButon = new ButtonTouch("OK");

		okButon.addActionListener(new ListenerFillParamOkButton(this));

		ButtonTouch cancelBouton = new ButtonTouch("Cancel");
		cancelBouton.addActionListener(new ListenerCancelButton(this));

		control.add(okButon);
		control.add(cancelBouton);

		this.getContentPane().add(content, BorderLayout.CENTER);
		this.getContentPane().add(control, BorderLayout.SOUTH);
	}

	private JPanel buildOptionsPanel(){
		optionsPanel = new JPanel(new GridBagLayout());
		optionsPanel.setPreferredSize(Constants.CAMERA_PANEL_DIMENSION_OPTIONS_PANEL);

		startSlider = new SliderTouch(SliderTouch.VERTICAL);
		endSlider = new SliderTouch(SliderTouch.VERTICAL);
		Object valStart = paramControl.getDisplayDataControl().getDataSet().getValueFromFrame(paramControl.getParamsControl().getDisplayDataList().getDisplayDataSelection().getSelMinAbs(), true);
		Object valEnd = paramControl.getDisplayDataControl().getDataSet().getValueFromFrame(paramControl.getParamsControl().getDisplayDataList().getDisplayDataSelection().getSelMaxAbs(), false);

		int intStart = -1, intEnd=-1;
		if(valStart instanceof Integer){
			intStart = (Integer)valStart;
			intEnd = (Integer)valEnd;
		}
		else if (valStart instanceof Double){
			intStart = (int) Math.round((Double)valStart);
			intEnd = (int) Math.round((Double)valEnd);
		}
		else if(valStart instanceof ValueAndString){
			intStart = ((ValueAndString)valStart).getValue();
			intEnd = ((ValueAndString)valEnd).getValue();
		}
		else{
			System.err.println("JDialogFillParam: buildOptionsPanel: ni int, ni double, ni valueAndString");
		}
		if(intStart==-1)intStart=0;
		if(intEnd==-1)intEnd=0;
		setY1(intStart);
		setY2(intEnd);

		startSlider.setValue(intStart);
		endSlider.setValue(intEnd);

		startField = new JTextField();
		startField.setEditable(false);
		endField = new JTextField();
		endField.setEditable(false);

		if(editParam.equals(E_EditParamsSliders.DYNAMICITY)){
			startSlider.setInverted(true);
			endSlider.setInverted(true);
		}

		if(editParam.isIntegerValue()){
			startField = new IntegerField();
			endField = new IntegerField();
			startField.addKeyListener(new ListenerIntegerField((IntegerField) startField, startSlider));
			endField.addKeyListener(new ListenerIntegerField((IntegerField) endField, endSlider));
		}
		else{
			startField = new JTextField();
			endField = new JTextField();
		}

		startField.setHorizontalAlignment(JTextField.CENTER);
		startField.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		startField.setPreferredSize(Constants.CAMERA_PANEL_DIMENSION_FIELD);
		startField.setFont(Constants.CAMERA_PANEL_FONT_SLIDERS_VALUES);
		startField.setEditable(editParam.isIntegerValue());

		endField.setHorizontalAlignment(JTextField.CENTER);
		endField.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		endField.setPreferredSize(Constants.CAMERA_PANEL_DIMENSION_FIELD);
		endField.setFont(Constants.CAMERA_PANEL_FONT_SLIDERS_VALUES);
		endField.setEditable(editParam.isIntegerValue());

		startSlider.setPreferredSize(Constants.CAMERA_PANEL_DIMENSION_SLIDERS_V);
		startSlider.setVisible(true);
		startSlider.setMinimum(editParam.getMinValue());
		startSlider.setMaximum(editParam.getMaxValue());
		startSlider.setMajorTickSpacing(editParam.getMajorTickSpacing());
		startSlider.setPaintTrack(true);
		startSlider.setSnapToTicks(editParam.isSnapToTicks());
		startSlider.setToolTipText(editParam.getSliderToolTip());
		startSlider.addChangeListener(new ListenerFillParamSliderStartChange(this));
		startSlider.setPaintLabels(true);

		endSlider.setPreferredSize(Constants.CAMERA_PANEL_DIMENSION_SLIDERS_V);
		endSlider.setVisible(true);
		endSlider.setMinimum(editParam.getMinValue());
		endSlider.setMaximum(editParam.getMaxValue());
		endSlider.setMajorTickSpacing(editParam.getMajorTickSpacing());
		endSlider.setPaintTrack(true);
		endSlider.setSnapToTicks(editParam.isSnapToTicks());
		endSlider.setToolTipText(editParam.getSliderToolTip());
		endSlider.addChangeListener(new ListenerFillParamSliderEndChange(this));
		endSlider.setPaintLabels(true);

		Hashtable<Integer, JLabel> labelTable = QualitativeSliders.getLabelTable(editParam);

		int startSliderValue = startSlider.getValue();
		int endSliderValue = endSlider.getValue();
		String startSliderValueString, endSliderValueString;;
		if(labelTable!=null && labelTable.get(startSliderValue)!=null && labelTable.get(endSliderValue)!=null){
			startSlider.setLabelTable(labelTable);
			endSlider.setLabelTable(labelTable);
			startSliderValueString = labelTable.get(startSliderValue).getText();
			endSliderValueString = labelTable.get(endSliderValue).getText();
			if(startSliderValueString.length()>Constants.CAMERA_PANEL_NB_LETTERS_FOR_SLIDERS_LABEL_TABLE){
				startSliderValueString=startSliderValueString.substring("<htlm><center>".length(),Constants.CAMERA_PANEL_NB_LETTERS_FOR_SLIDERS_LABEL_TABLE+"<html><center>".length());
			}
			if(endSliderValueString.length()>Constants.CAMERA_PANEL_NB_LETTERS_FOR_SLIDERS_LABEL_TABLE){
				endSliderValueString=endSliderValueString.substring("<htlm><center>".length(),Constants.CAMERA_PANEL_NB_LETTERS_FOR_SLIDERS_LABEL_TABLE+"<html><center>".length());
			}
			startField.setText(startSliderValueString);
			endField.setText(endSliderValueString);
			startField.setForeground(labelTable.get(startSliderValue).getForeground());
			endField.setForeground(labelTable.get(endSliderValue).getForeground());
			startField.setBorder(BorderFactory.createLineBorder(startField.getForeground()));
			endField.setBorder(BorderFactory.createLineBorder(endField.getForeground()));
		}
		else{
			startSliderValueString = Integer.toString(startSliderValue);
			startField.setText(startSliderValueString);
			endSliderValueString = Integer.toString(endSliderValue);
			endField.setText(endSliderValueString);
		}

		startLabel = new JLabel("Start:");
		endLabel = new JLabel("End:");


		int interpolationStartValue=50;
		setInterpolationUpValue(interpolationStartValue);
		setInterpolationDownValue(interpolationStartValue);

		interpolationSliderUp = new SliderTouch(SliderTouch.HORIZONTAL);
		interpolationSliderUp.setValue(interpolationStartValue);
		interpolationSliderUp.setPreferredSize(Constants.CAMERA_PANEL_DIMENSION_SLIDER_H);
		interpolationSliderUp.setVisible(true);
		interpolationSliderUp.setMinimum(0);
		interpolationSliderUp.setMaximum(100);
		interpolationSliderUp.setMajorTickSpacing(25);
		interpolationSliderUp.setPaintTrack(true);
		interpolationSliderUp.setSnapToTicks(false);
		interpolationSliderUp.setToolTipText("Play with the up transition curvature");
		interpolationSliderUp.addChangeListener(new ListenerFillParamSliderInterpolationUpChange(this));
		interpolationSliderUp.setPaintLabels(true);
		
		interpolationSliderDown = new SliderTouch(SliderTouch.HORIZONTAL);
		interpolationSliderDown.setValue(interpolationStartValue);
		interpolationSliderDown.setPreferredSize(Constants.CAMERA_PANEL_DIMENSION_SLIDER_H);
		interpolationSliderDown.setVisible(true);
		interpolationSliderDown.setMinimum(0);
		interpolationSliderDown.setMaximum(100);
		interpolationSliderDown.setMajorTickSpacing(25);
		interpolationSliderDown.setPaintTrack(true);
		interpolationSliderDown.setSnapToTicks(false);
		interpolationSliderDown.setToolTipText("Play with the down transition curvature");
		interpolationSliderDown.addChangeListener(new ListenerFillParamSliderInterpolationDownChange(this));
		interpolationSliderDown.setPaintLabels(true);

		bezierComponent = new BezierComponent(interpolationStartValue, Constants.CAMERA_PANEL_DIMENSION_BEZIER_COMPONENT);

		GridBagConstraints c = new GridBagConstraints();
		c.gridx=0;
		c.gridy=0;

		optionsPanel.add(startLabel,c);

		c.gridy+=1;
		optionsPanel.add(startSlider,c);

		c.gridy+=1;
		optionsPanel.add(startField,c);

		c.gridx+=1;
		c.gridy=0;
		optionsPanel.add(endLabel,c);

		c.gridy+=1;
		optionsPanel.add(endSlider,c);

		c.gridy+=1;
		optionsPanel.add(endField,c);
		
		c.gridy=0;
		c.gridx=2;
		c.gridheight=2;
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(interpolationSliderUp,BorderLayout.NORTH);
		panel.add(bezierComponent, BorderLayout.CENTER);
		panel.add(interpolationSliderDown, BorderLayout.SOUTH);
		optionsPanel.add(panel,c);
		this.setSize(Constants.CAMERA_PANEL_DIMENSION_DIALOG);
		
		return optionsPanel;
	}

	public Integer getY1() {
		return y1;
	}

	public void setY1(Integer y1) {
		this.y1 = y1;
	}

	public Integer getY2() {
		return y2;
	}

	public void setY2(Integer y2) {
		this.y2 = y2;
	}

	public JTextField getStartField() {
		return startField;
	}

	public JTextField getEndField() {
		return endField;
	}

	public BezierComponent getBezierComponent() {
		return bezierComponent;
	}

	public Integer getInterpolationUpValue() {
		return interpolationUpValue;
	}

	public void setInterpolationUpValue(Integer interpolationUpValue) {
		this.interpolationUpValue = interpolationUpValue;
	}

	public Integer getInterpolationDownValue() {
		return interpolationDownValue;
	}

	public void setInterpolationDownValue(Integer interpolationDownValue) {
		this.interpolationDownValue = interpolationDownValue;
	}

}
